<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class IndexController extends BaseController {

	//首页
	public function index() {
        $this->display();
    }

    //个人信息
    public function info(){
        if (I('post.action') == 'adminauth_edit_action') {
            unset($_POST['action']);
            $finfo = upload($_FILES['avatar']);
            if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
            	$this->error($finfo['msg']);
            } elseif ($finfo['status'] == 1) {
            	$_POST['admin_avatar'] = $finfo['filepath'];
            }
            if(M('admin')->save($_POST)){
                $data['admin_id'] = $_POST['admin_id'];
                $admininfo = M('admin')->alias('a')->join('left join midi_auth_group_access as ga on a.admin_id = ga.uid')->join('left join midi_auth_group as g on ga.group_id = g.id')->where($data)->find();
                unset($admininfo['admin_password']);
                session('midiadmin',$admininfo);
                $this->success('修改个人资料成功');
            }else{
                $this->error('没有个人资料被修改');
            }
            exit();
        }
        //session信息
        $data['admin_id'] = $_SESSION['midiadmin']['admin_id'];
        $admin_info = M('admin')->alias('a')->join('left join midi_auth_group_access as ga on a.admin_id = ga.uid')->join('left join midi_auth_group as g on ga.group_id = g.id')->where($data)->find();
        //权限信息
        $auth_arr = M('auth_rule')->field('id,title')->order('category asc')->select();
        $count = M('auth_rule')->count('id');
        $cols = ceil($count/5);
        $rule_arr = explode(',', $admin_info['rules']);

        $this->assign('admin_info',$admin_info);
        $this->assign('auth_arr',$auth_arr);
        $this->assign('cols',$cols);
        $this->assign('rule_arr',$rule_arr);
        $this->display();
    }

    //修改密码
    public function pwd(){
        if (I('post.action') == 'edit_pwd') {
        	unset($_POST['action']);
        	if ($_POST['new_password'] != $_POST['new_repassword']) {
        		$this->error('输入的两次新密码不一致！');
        	}
        	$data['admin_id'] = $_POST['admin_id'];
        	$data['admin_password'] = md5($_POST['admin_password'].C(AES_KEY));
        	if (!M('admin')->where($data)->find()) {
        		$this->error('原密码输入错误！');
        	}
        	$data['admin_password'] = md5($_POST['new_password'].C(AES_KEY));
        	if (M('admin')->save($data)) {
        		unset($_SESSION['midiadmin']);
        		$this->success('修改密码成功，请重新登录！');
        	}else{
        		$this->error('密码没有被修改！');
        	}
        	exit();
        }
        $this->display();
    }
}